package com.k2.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.k2.common.exception.BizCodeEnume;
import com.k2.common.vo.UserRegisterVo;
import com.k2.gulimall.member.entity.MemberLevelEntity;
import com.k2.gulimall.member.service.MemberLevelService;
import com.k2.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.service.MemberService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;

import javax.servlet.http.HttpSession;

import static com.k2.common.constant.AuthServerConstant.LOGIN_USER;


/**
 * 会员
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberLevelService memberLevelService;

    @GetMapping(value = "/oauth2/login")
    public R oauthLogin(@RequestParam Map<String,String> jsonMap) throws Exception {
        SocialUser socialUser = JSON.parseObject(jsonMap.get("socialUser"),SocialUser.class);
        UserRegisterVo userRegisterVo = JSON.parseObject(jsonMap.get("userRegisterVo"),UserRegisterVo.class);
        MemberEntity memberEntity = memberService.login(socialUser, userRegisterVo);
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
    }

    @GetMapping(value = "/weixin/login")
    public R weixinLogin(@RequestParam Map<String,String> jsonMap) {
        String accessTokenInfo = jsonMap.get("accessTokenInfo");
        UserRegisterVo userRegisterVo = JSON.parseObject(jsonMap.get("userRegisterVo"),UserRegisterVo.class);
        MemberEntity memberEntity = memberService.login(accessTokenInfo,userRegisterVo);
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        } else {
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        String levelnamme = (String) params.get("levelnamme");
        List<MemberLevelEntity> memberLevelEntities = memberLevelService.getLevelNameToMemberLevel(levelnamme);
        if (memberLevelEntities.size()>0){
            params.put("levelnamme",memberLevelEntities.get(0).getId());
        }else{
            params.put("levelnamme","");
        }
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        //等级

        //积分

		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public R save(@RequestBody MemberEntity member){
		memberService.saveOrUpdate(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据用户名查询用户
     */
    @RequestMapping(value = "/info/name",method = RequestMethod.POST)
    public R getUserName(@RequestBody MemberEntity member){
        List<MemberEntity> memberEntityList = memberService.getUserName(member);
        Map<String,Object> map = new HashMap<>();
        if (memberEntityList.size()>0){
            map.put("data",memberEntityList.get(0));
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"请输入正确的用户名");
        }
    }

    @RequestMapping("/info/email")
    public R getUserEmail(@RequestBody MemberEntity member){
        List<MemberEntity> memberEntityList = memberService.getUserEmail(member);
        Map<String,Object> map = new HashMap<>();
        if (memberEntityList.size()>0){
            map.put("data",memberEntityList.get(0));
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"请输入正确的邮箱");
        }
    }

    @RequestMapping("/info/mobile")
    public R getUserMobile(@RequestBody MemberEntity member){
        List<MemberEntity> memberEntityList = memberService.getUserMobile(member);
        Map<String,Object> map = new HashMap<>();
        if (memberEntityList.size()>0){
            map.put("data",memberEntityList.get(0));
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"请输入正确的手机号");
        }
    }

    /**
     * 批量删除用户
     * @param params
     * @return
     */
    @PostMapping("/batchdeletemember")
    public R batchdeletemember(@RequestParam Map<String, Object> params){
        Map<String,Object> map = new HashMap<>();
        String ids = (String) params.get("ids[]");
        if(ids==null||ids.length()==0){
            ids = (String) params.get("ids");
        }
        Integer status = Integer.parseInt((String) params.get("status"));
        if(memberService.batchdelete(ids,status)){
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"请求参数错误");
        }
    }



}
